<template>
    <el-main>
        <el-form :model="listParam" label-width="80px" :inline="true" size="default">
            <el-form-item>
                <el-input v-model="listParam.mingCheng" placeholder="请输入名称"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button :icon="Search" @click="searchBtn">搜索</el-button>
                <el-button :icon="Close" type="danger" plain @click="resetBtn">重置</el-button>
                <el-button type="primary" :icon="Plus" @click="addBtn">新增</el-button>
            </el-form-item>
        </el-form>
        <!-- 用户列表 -->
        <el-table :height="tableHeight" :data="tableList" border stripe>
            <el-table-column label="名称" prop="mingCheng"></el-table-column>
            <el-table-column label="型号" prop="xingHao"></el-table-column>
            <el-table-column label="尺寸" prop="chiCun"></el-table-column>
            <el-table-column label="重量" prop="zhongLiang"></el-table-column>
            <el-table-column label="交货周期" prop="jiaoHuoZhouQi"></el-table-column>
            <el-table-column label="附件">
                <template #default="scope">
                    <!-- <el-link type="primary" :underline="false" :href="scope.row.fuJian"
                        target="_blank">{{ scope.row.fuJian }}</el-link> -->
                    <a href="#" @click.prevent="viewFile()">{{ scope.row.fuJian }}</a>
                    <!-- <el-link type="primary" @click="viewFile(scope.row.fuJian)">
                            {{ scope.row.fuJian }}
                        </el-link> -->
                </template>
            </el-table-column>
            <el-table-column label="备注" prop="beiZhu"></el-table-column>

            <el-table-column label="操作" width="230">
                <template #default="scope">
                    <el-button type="primary" :icon="Edit" size="default" @click="editBtn(scope.row)">编辑</el-button>
                    <el-button type="danger" :icon="Delete" size="default" @click="deleteBtn(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-main>
    <!-- 新增和编辑弹框 -->
    <AddUser ref="addRef" @onFresh="getList"></AddUser>
</template>

<script setup lang="ts">
import { Search, Close, Plus, Edit, Delete } from "@element-plus/icons-vue";
import useUser from "@/composables/product/useProduct";
import AddUser from './AddProduct.vue';
import useProductTable from "@/composables/product/usePoductTable";

const { listParam, getList, resetBtn, searchBtn, tableList, tableHeight } = useProductTable(); 
const { addBtn, deleteBtn, editBtn, addRef } = useUser(getList);  

 
const getFileUrl = (fileName: string): string => {
    return `http://localhost:8080/uploads/${fileName}`;
};

// 查看文件的方法
const viewFile = () => {
    // 查找匹配的项
    const foundItem = tableList.value[0].fileUrl;
    console.log('Found Item:', foundItem);
    if (foundItem) {
        console.log('File URL:', foundItem.fileUrl);  
        // let url = `http://localhost:8080/uploads/${foundItem}`;
        let url = getFileUrl(foundItem);
        console.log(url);
        window.open(url, '_blank');
    } else {
        console.log('No valid fileUrl found for the given fileName');
    }
};
</script>

<style scoped></style>